/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.productionPlanning.session;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import mrp.productionPlanning.entity.WeeklyDetailEntity;

/**
 *
 * @author Wei Haoyang
 */
@Stateless
@LocalBean
public class WeeklyDetailSessionBean {

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    
    @PersistenceContext
    private EntityManager em;
    private WeeklyDetailEntity wd;
    private SimpleDateFormat formatter = new SimpleDateFormat("dd/mm/yyyy");
    @EJB
    private DailyDetailSessionBean ddsb;

    public WeeklyDetailEntity getWeeklyDetailByDate(Date d){
        Timestamp ts = new Timestamp(d.getTime());
        Query query  = em.createQuery("SELECT u FROM WeeklyDetailEntity u where u.ts = :dd");
        query.setParameter("dd",ts);
        WeeklyDetailEntity wd = null;
        try{
            wd = (WeeklyDetailEntity)query.getSingleResult();
        }
        catch(NoResultException ex){
            System.out.println("No Result");
//            throw ex;
        }
        return wd;
    }
    
    public WeeklyDetailEntity getWeeklyDetailById(Long id){
        return em.find(WeeklyDetailEntity.class,id);
    }
    
    public void addWeeklyDetail(WeeklyDetailEntity wd){
        wd.getTs().setTime(wd.getDate().getTime());
        em.persist(wd);
    }
    
    public void updateWeeklyDetail(WeeklyDetailEntity wd){
        wd.getTs().setTime(wd.getDate().getTime());
        em.merge(wd);
    }
    
//    public void setDays(WeeklyDetailEntity wd){
//        Date d = wd.getDate();
//        Calendar cal = new GregorianCalendar();
//        cal.setTime(d);
//        int i = 0;
//        DailyDetailEntity dd;
//        while (i<7 && cal.YEAR == d.getYear()){
//            dd = ddsb.getDailyDetailByDate(d);
//            wd.getDays().add(dd);
//            cal.add(Calendar.DATE, 1);
//            i++;
//        }
//    }
    
    public void generateAYear(int year) throws ParseException{
        Date d = null;
        Calendar cal = new GregorianCalendar();
        String s = "01/01/"+ year;
        try {
            d = formatter.parse(s);
        }
        catch (ParseException e){
            e.printStackTrace();
        }
        cal.setTime(d);
        WeeklyDetailEntity lastW = new WeeklyDetailEntity();
        lastW.setDate(d);
        
        Date temp = null;
        while ((cal.getTime().getYear()+1900) == year){
            temp = cal.getTime();
            if (cal.get(Calendar.DAY_OF_WEEK) == 1 || cal.get(Calendar.DAY_OF_WEEK) == 7){
                cal.add(Calendar.DATE, 1);
            }
            else{
                if (cal.get(Calendar.DAY_OF_WEEK) == 2){
                    lastW = new WeeklyDetailEntity();
                    lastW.setDate(cal.getTime());
                    lastW.getDays().add(ddsb.getDailyDetailByDate(cal.getTime()));
                    addWeeklyDetail(lastW);
                    cal.add(Calendar.DATE, 1);
                }
                else{
                    lastW.getDays().add(ddsb.getDailyDetailByDate(cal.getTime()));
                    updateWeeklyDetail(lastW);
                    cal.add(Calendar.DATE, 1);
                }
            }
        }
        return;
    }
    
    public void removeWeeklyDetailByDate(Date d){
        em.remove(getWeeklyDetailByDate(d));
    }

    public WeeklyDetailEntity getWd() {
        return wd;
    }

    public void setWd(WeeklyDetailEntity wd) {
        this.wd = wd;
    }

    public DailyDetailSessionBean getDdsb() {
        return ddsb;
    }

    public void setDdsb(DailyDetailSessionBean ddsb) {
        this.ddsb = ddsb;
    }
    
    public int countWorkDays(WeeklyDetailEntity wd){
        return wd.getDays().size();
    }
    
}
